package com.globallogic.socialnetwork.jsf.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.inject.Named;
import javax.jms.JMSException;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;

import com.globallogic.socialnetwork.entity.Message;
import com.globallogic.socialnetwork.service.MessageConn;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.*;

/**
 * JSF Bean to handle all functions associated with messages
 */
@Named("msgBean")
@Scope("request")
public class MessageBean {

	@EJB(mappedName = "java:module/UserService")
	private UserService userService;
	private String userLogin;
	private String receiver;
	private String message;
	private List<Message> messages;
	private boolean haveUnreadMessage;
	private Message selectedMessage;

	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	public MessageBean() {

		userLogin = WebApplication.getSessionAttribute("userlogin").toString();
		log.warn("is here " + haveUnreadMessage);
	}

	/**
	 * Send message to <receiver> with content <message>
	 * 
	 * @return "home?faces-redirect=true"
	 * @throws NamingException
	 * @throws JMSException
	 */
	public String sendMessage() {
		new MessageConn(userLogin, receiver, message);
		return "home?faces-redirect=true";
	}

	/**
	 * Removes selected message
	 */
	public void removeMessage() {
		try {
			if (selectedMessage != null)
				userService.removeMessage(selectedMessage);
		} catch (DaoException e) {
			log.warn(e);
		}
	}

	/**
	 * Marks as read message <selectedMessage>
	 */
	public void markAsRead() {
		try {
			userService.markAsRead(selectedMessage);
		} catch (DaoException e) {
			log.warn(e);
		}

	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public List<Message> getMessages() {
		try {
			messages = userService.getAllMessages(userLogin);
		} catch (DaoException e) {
			log.warn(e);
		}
		return messages;
	}

	public Message getSelectedMessage() {
		return selectedMessage;
	}

	public void setSelectedMessage(Message selectedMessage) {
		this.selectedMessage = selectedMessage;
	}

	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

	public boolean getHaveUnreadMessage() {
		getMessages();
		if (messages != null)
			for (Message msg : messages) {
				if (msg.getIsNew()) {
					haveUnreadMessage = true;
					return haveUnreadMessage;
				}
			}
		haveUnreadMessage = false;
		return haveUnreadMessage;
	}

	public void setHaveUnreadMessage(boolean haveUnreadMessage) {
		this.haveUnreadMessage = haveUnreadMessage;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

}
